package i_Inst;

import mips.Decoder;
import mips.Memory;
import mips.PC;
import mips.Register;

public class LW {
	public static void lw(int destination,int source,int address){
		Register.registers[destination] = Memory.dataMemory.get(address + Register.registers[source]);
		
		System.out.println("PC value now is : " +Decoder.toBinary(PC.pc)); 
		
		System.out.println("PC value :"+ Decoder.toBinary(PC.pc)+" sent to The instruction memory to read address..");
		
		System.out.println("Instuction memory sending Instruction[31:26] to CONTROL,Instruction[31:26] having value '100011'");
		System.out.println("Instuction memory sending Instruction[25:21] : "+Decoder.AddzerostoLeft(Decoder.toBinary(source)+"",5)+" to Registers @ read Register1");
		System.out.println("CONTROL send RegDst of 00 to the mux");
		System.out.println("The mux have selected 0");
		System.out.println("Instruction memory sending Instruction[20:16] : "+Decoder.AddzerostoLeft(Decoder.toBinary(destination)+"",5)+" to Registers @ write Register");
	
		System.out.println("Register number  read now is : " + Decoder.AddzerostoLeft(Decoder.toBinary(source)+"",5));
		System.out.println("Destination register  is : " + Decoder.AddzerostoLeft(Decoder.toBinary(destination)+"",5));
		
		System.out.println("CONTROL send ALUOP of 000 to ALU CONTROL");
		
		System.out.println("Instruction  memory sending Instruction[15:0] to Sign Extend...");
		System.out.println("The address after Sign Extend: " + Decoder.AddzerostoLeft(Decoder.toBinary(address)+"",32));
		
		System.out.println("CONTROL send ALUSrc of 01 to the mux");
		System.out.println("The mux have selected 1");
		
		System.out.println("Registers sends first source value : "+ Decoder.toBinary(source) + " Second Source value : " + Decoder.toBinary(address) + "  to ALU");
		System.out.println("ALU is adding the content of register 1 to address now...");
		System.out.println("Address: " + Register.registers[destination] + " is went to Data Memory");
		System.out.println("Result is ready :" + Decoder.toBinary(Register.registers[destination]));
		System.out.println("CONTROL send MemRead of 1 to Data Memory");
		System.out.println("CONTROL send MemToRead of 1 to selection of mux");
		System.out.println("mux selected 1..");
		System.out.println("Sending to the mux...");
		System.out.println(Decoder.toBinary(Register.registers[destination]) +" is sent now to the registers @ write data");
		System.out.println("Register : " +  Decoder.AddzerostoLeft(Decoder.toBinary(destination)+"",5) + ",have value now : " +Decoder.toBinary(Register.registers[destination]));
		
		System.out.println("PC value sent to Add");
		PC.pc +=4;
		System.out.println("pc value now is incremented by 4 and passed to the mux :"+Decoder.toBinary(PC.pc)+",selection 0 occured");
		System.out.println("Mux sent back to PC");
		System.out.println("New value of PC now is : " + Decoder.toBinary(PC.pc));
		System.out.println("------------------------------------");
	}
}
